var layer = layui.layer;
layui.use(function () {
	var $ = layui.$;
	var util = layui.util;
	// 事件
	util.on('lay-on', {
		'login': function () {
			layer.open({
				type: 1,
				scrollbar: false,
				skin: 'skin-pop',
				area: ['400px', '220px'],
				content: $('#login'),
			});
		},
		'vip': function () {
			layer.open({
				type: 1,
				scrollbar: false,
				skin: 'skin-pop',
				area: ['400px', '260px'], // 宽高
				content: $('#vip'),
			});
		},
	});
});
var checkHeight = $(".progressBar .active").offset().top;
var heightW = $(window).height();
if (checkHeight > (heightW/2)) {
	$('.content').animate({ scrollTop:checkHeight - (heightW/2)}, 100);
}
//展开收缩
const container = document.querySelector('.learning-containe');
const sidebar = document.querySelector('.learning-sidebar');
const content = document.querySelector('.learning-content');
const listitem = document.querySelector('.listitem');
const toggleText = document.querySelector('.switch');
toggleText.addEventListener('click', function () {
	if (container.classList.contains('hidden')) {
		container.classList.remove('hidden');
		sidebar.classList.remove('hidden');
		setTimeout(function () {
			listitem.classList.remove('hidden');
		}, 260);
		toggleText.innerHTML = '<i class="iconfont icon-left"></i>';
	} else {
		container.classList.add('hidden');
		sidebar.classList.add('hidden');
		listitem.classList.add('hidden');
		toggleText.innerHTML = '<i class="iconfont icon-right"></i>';
	}
});

//获取左侧高度
const titleH = document.querySelector('.learning-sidebar .listitem .title');
const stageSwitchH = document.querySelector('.learning-sidebar .listitem .stage-switch');
const contentH = document.querySelector('.learning-sidebar .listitem .content');

// 获取元素的高度
const tHeight = titleH.offsetHeight;
const sHeight = stageSwitchH.offsetHeight;

// 将高度存储在自定义属性中
contentH.style.setProperty('--t-height', `${tHeight}px`);
contentH.style.setProperty('--s-height', `${sHeight}px`);
$('.specific-left').click(function(){
	switchTabSpec('prev');
});
$('.specific-right').click(function(){
	switchTabSpec('next');
});
function switchTabSpec(status){
	var id = $('.content-panel').data('id');
	var sid = $('.content-panel').data('s');
	$.ajax({
		data: {id: id,'sid': sid,status: status},
		type: 'post',
		url: '/learning/lists',
		success: function(obj){
			$('.progressBar').html(obj.html);
			$('.content-panel').data('s',obj.s);
			$('.txt h3').text(obj.name);
		}
	});
}
turnPage(1);
function turnPage(page = 1)
{
	$.ajax({
		url: '/learning/queslists',
		type: 'post',
		data: {id: $('.content-panel').data('id'), page: page},
		success: function(obj){
			$('.questionlists').html(obj);
		}
	});
}
$(function(){
	setTimeout(function(){
		$('.syntaxhighlighter').each(function(i) { 
			var str = "<span class='copy_btn_code hide'>复制</span>";
			$(this).append(str);
		});
	}, 3000);
	$(document).on('mouseover','.syntaxhighlighter', function () {
		$(this).find('.copy_btn_code').removeClass('hide');
	});
	$(document).on('mouseleave','.syntaxhighlighter', function () {
		$(this).find('.copy_btn_code').addClass('hide');
	});
});
$(document).on('click','.copy_btn_code', function () {
	var c = $(this).prev().find(".container").find('.line');
	var d = "";
	$.each(c, function (a, b) {
		d += $(b).text() + "\n";
	});
	var input = document.createElement('textarea');
	input.value = $.htmlPrefilter(d);
	document.body.appendChild(input);  
	input.select(); // 选取文本域中的内容.
	document.execCommand("Copy");
	//再移除输入框
	document.body.removeChild(input);
	layer.msg("复制成功！");
});